package com.zkk.service.impl;

import com.zkk.dao.TransferAccountDao;
import com.zkk.dao.impl.TransferAccountDaoImpl;
import com.zkk.pojo.TransferAccount;
import com.zkk.service.TransferAccountService;
import com.zkk.utils.DruidUtil;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;

public class TransferAccountServiceImpl implements TransferAccountService {
    TransferAccountDao transferAccountDao = new TransferAccountDaoImpl();
    @Override
    public int add(TransferAccount transferAccount) {
        return transferAccountDao.add(transferAccount);
    }

    @Override
    public int transferMoney(String card_no, BigDecimal money) {
        Connection connection = DruidUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            int i = transferAccountDao.transferMoney(card_no, money);
            connection.commit();
            return i;
        } catch (Exception e) {
            System.out.println("收款失败");
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }
        return 0;

    }

    @Override
    public TransferAccount queryByCard_no(String card_no) {
        return transferAccountDao.queryByCard_no(card_no);
    }
}
